package com.lovelyj.trip_by_train.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovelyj.trip_by_train.entity.Arrive;
import com.lovelyj.trip_by_train.entity.Coach;
import com.lovelyj.trip_by_train.entity.DailyTrain;
import com.lovelyj.trip_by_train.entity.Train;
import com.lovelyj.trip_by_train.mapper.TrainMapper;
import com.lovelyj.trip_by_train.service.ArriveService;
import com.lovelyj.trip_by_train.service.CoachService;
import com.lovelyj.trip_by_train.service.DailyTrainService;
import com.lovelyj.trip_by_train.service.TrainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Slf4j
@Service
public class TrainServiceImpl extends ServiceImpl<TrainMapper, Train> implements TrainService {

    @Autowired
    private ArriveService arriveService;

    @Autowired
    private CoachService coachService;

    @Override
    @Transactional
    public void savePro(Train train){
        this.save(train);
        Arrive arrive0 = new Arrive();      //起点arrive
        arrive0.setTrainNum(train.getTrainNum());
        arrive0.setDepartureTime(train.getStartTime());
        arrive0.setArriveTime(train.getStartTime());
        arrive0.setStation(train.getStartStation());
        arrive0.setDepartureDay(0);
        arrive0.setArriveDay(0);
        arrive0.setDistance(0);
        arriveService.save(arrive0);
        Arrive arrive1 = new Arrive();      //终点arrive
        arrive1.setTrainNum(train.getTrainNum());
        arrive1.setArriveTime(train.getEndTime());
        arrive1.setDepartureTime(train.getEndTime());
        arrive1.setStation(train.getEndStation());
        arrive1.setArriveDay(train.getDateDiff());
        arrive1.setDepartureDay(train.getDateDiff());
        arrive1.setDistance(train.getDistance());
        arriveService.save(arrive1);
    }

    @Override
    @Transactional
    public void updatePro(Train train){
        Train originalTrain = this.getById(train.getId());

        LambdaUpdateWrapper<Coach> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.eq(Coach::getTrainNum, originalTrain.getTrainNum());
        updateWrapper1.set(Coach::getTrainNum, train.getTrainNum());
        coachService.update(updateWrapper1);

        LambdaQueryWrapper<Arrive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Arrive::getTrainNum, originalTrain.getTrainNum());
        queryWrapper.eq(Arrive::getStation, originalTrain.getStartStation());
        arriveService.remove(queryWrapper);
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Arrive::getTrainNum, originalTrain.getTrainNum());
        queryWrapper.eq(Arrive::getStation, originalTrain.getEndStation());
        arriveService.remove(queryWrapper);

        if(!Objects.equals(originalTrain.getTrainNum(), train.getTrainNum())){
                LambdaUpdateWrapper<Coach> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(Coach::getTrainNum, originalTrain.getTrainNum()).set(Coach::getTrainNum, train.getTrainNum());
                coachService.update(updateWrapper);
        }

        this.updateById(train);

        Arrive arrive0 = new Arrive();      //起点arrive
        arrive0.setTrainNum(train.getTrainNum());
        arrive0.setDepartureTime(train.getStartTime());
        arrive0.setArriveTime(train.getStartTime());
        arrive0.setStation(train.getStartStation());
        arrive0.setDepartureDay(0);
        arrive0.setArriveDay(0);
        arrive0.setDistance(0);
        arriveService.save(arrive0);
        Arrive arrive1 = new Arrive();      //终点arrive
        arrive1.setTrainNum(train.getTrainNum());
        arrive1.setArriveTime(train.getEndTime());
        arrive1.setDepartureTime(train.getEndTime());
        arrive1.setStation(train.getEndStation());
        arrive1.setArriveDay(train.getDateDiff());
        arrive1.setDepartureDay(train.getDateDiff());
        arrive1.setDistance(train.getDistance());
        arriveService.save(arrive1);
    }

}
